package com.meihua.baoleyi.base;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.meihua.baoleyi.common.ActivityManager;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.view.CatLoadingView;

public class BaseActivity extends FragmentActivity implements View.OnClickListener {

    private static final String TAG = "BaseActivity";

    public MyApp myApp;

    private CatLoadingView loadingDialog; //加载视图

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//		PushAgent.getInstance(this).onAppStart();
        ActivityManager.getInstance().addActivity(getClass().getSimpleName(), this);
        myApp = (MyApp) getApplication();
        MyApp.getInstance().addActivity(this);
    }

    /**
     * 以satartActivityForResult方式启动另一个Activity
     *
     * @param classObj    class 对象
     * @param requestCode 请求码
     */
    public void overlay(Class<?> classObj, int requestCode) {
        Intent intent = new Intent(this, classObj);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 带有Bundle参数的跳转到另一个activity
     *
     * @param classObj 要跳转的activity
     * @param bundle   要传递的Bundle
     */
    public void overlay(Class<?> classObj, Bundle bundle) {
        Intent intent = new Intent(this, classObj);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 直接跳转到另一个activity
     *
     * @param classObj 要跳转的activity
     */
    public void overlay(Class<?> classObj) {
        Intent intent = new Intent(this, classObj);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onResume() {
        super.onResume();
//        ToastUtil.show(this, getClass().getSimpleName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loadingDialog = null; //防止activity已经finish掉 再去执行dismissDialog()
        MyApp.getInstance().removeActivity(this);
        ActivityManager.getInstance().removeActivity(getClass().getSimpleName());
    }

    /**
     * 隐藏软键盘
     */
    public void hiddenInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            //隐藏
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
        }
    }

    /**
     * 显示加载视图
     */
    protected void showDialog() {
        try {
            if (loadingDialog == null) {
                loadingDialog = new CatLoadingView();
            }
            if (!loadingDialog.isVisible()) {
                loadingDialog.show(getSupportFragmentManager(), TAG);
            }
        } catch (Exception e) {
        }
    }

    /**
     * 移除等待视图
     */
    protected void dismissDialog() {
        try {
            if (loadingDialog == null) {
                return;
            }
            loadingDialog.dismiss();
            loadingDialog = null;
        } catch (Exception e) {
        }
    }

}
